package com.luying.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luying.dao.ClassDao;
import com.luying.dao.ClassSubjectDao;
import com.luying.dao.MoralDao;
import com.luying.dao.SubjectDao;
import com.luying.pojo.*;
import com.luying.pojo.Class;
import com.luying.service.BasicInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BasicInfoServiceImpl implements BasicInfoService {
    @Autowired
    private ClassDao classDao;
    @Autowired
    private SubjectDao subjectDao;
    @Autowired
    private ClassSubjectDao classSubjectDao;
    @Autowired
    private MoralDao moralDao;
    @Override
    public PageResult queryClass(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageIndex(),pageQuery.getPageSize());
        List<Class> list =  classDao.selectPage(pageQuery);
        PageInfo<Class> pageInfo = new PageInfo(list);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),list);
        return pageResult;
    }

    @Override
    public int addClass(Class aClass) throws Exception {
        Example example = new Example(Class.class);
        example.createCriteria().andEqualTo("className",aClass.getClassName());
        List<Class> classes = classDao.selectByExample(example);
        if(aClass!=null&&classes.size()>0){
            throw  new Exception("已有该名称的班级,请勿重复添加!");
        }
        return classDao.insertSelective(aClass);
    }

    @Override
    public int deleteClass(Integer id) throws Exception {
        //查询在班级科目关系是否有该班级在使用
        int count = classDao.selectClassAndSubjectByClassId(id);
        if(count>0){
            throw new Exception("该班级在班级和科目关系中有使用,禁止删除!");
        }
        //查询在老师工作关系中是否有该班级在使用
        int count1 = classDao.selectTeacherJobByClassId(id);
        if(count1>0){
            throw new Exception("该班级在老师工作关系中中有使用,禁止删除!");
        }
        //查询该班级下是否有学生
        int count2 = classDao.selectStudentByClassId(id);
        if(count2>0){
            throw new Exception("该班级下有学生,禁止删除!");
        }
        return classDao.deleteById(id);
    }

    @Override
    public int updateClass(Class aclass) {
        return classDao.updateById(aclass);
    }

    @Override
    public int addUser(User user, Integer type) {
        return 0;
    }

    @Override
    public int addSubject(Subject subject) {
        return subjectDao.insertSelective(subject);
    }

    @Override
    public PageResult querySubject(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageIndex(),pageQuery.getPageSize());
        List<Subject> list =  subjectDao.selectPage(pageQuery);
        PageInfo<Subject> pageInfo = new PageInfo(list);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),list);
        return pageResult;
    }

    @Override
    public int deleteSubject(Integer id) throws Exception {
        //查询在班级科目关系是否有该科目在使用
        int count = classDao.selectClassAndSubjectBySubjectId(id);
        if(count>0){
            throw new Exception("该科目在班级和科目关系中有使用,禁止删除!");
        }
        //查询在老师工作关系中是否有该科目在使用
        int count1 = classDao.selectTeacherJobBySubjectId(id);
        if(count1>0){
            throw new Exception("该科目在老师工作关系中中有使用,禁止删除!");
        }
        return subjectDao.deleteById(id);
    }

    @Override
    public int updateSubject(Subject subject) {
        return subjectDao.updateById(subject);
    }

    @Override
    public void updateClassAndSubject(ClassAndSubjectVo classAndSubject) {
        Example example = new Example(ClassSubject.class);
        example.createCriteria().andEqualTo("classId",classAndSubject.getClassId());
        List<ClassSubject> classSubjects = classSubjectDao.selectByExample(example);
        if(classSubjects!=null&&classSubjects.size()>0){
            //删除
            classSubjectDao.deleteByExample(example);
        }
        //新增
        if(classAndSubject.getSubjectList()!=null&&classAndSubject.getSubjectList().size()>0){
            classSubjectDao.batch(classAndSubject.getClassId(),classAndSubject.getSubjectList());
        }
    }

    @Override
    public Map<String, Object> classAndSubjectList() {
        Map<String,Object> resp = new HashMap<>(16);
        List<Class> classes = classDao.selectAll();
        List<Subject> subjects = subjectDao.selectAll();
        resp.put("classes",classes);
        resp.put("subjects",subjects);
        return resp;
    }

    @Override
    public PageResult classAndSubjectQuery(PageQuery query) {
        PageHelper.startPage(query.getPageIndex(),query.getPageSize());
        List<ClassAndSubjectVo> list =  subjectDao.selectClassAndSubject(query);
        PageInfo<ClassAndSubjectVo> pageInfo = new PageInfo(list);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),list);
        return pageResult;
    }

    @Override
    public int addMoral(Moral moral) {
        return moralDao.insertSelective(moral);
    }

    @Override
    public PageResult queryMoral(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageIndex(),pageQuery.getPageSize());
        List<Moral> list =  moralDao.selectPage(pageQuery);
        PageInfo<Moral> pageInfo = new PageInfo(list);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),list);
        return pageResult;
    }

    @Override
    public int deleteMoral(Integer id) {
        return moralDao.deleteById(id);
    }

    @Override
    public int updateMoral(Moral moral) {
        return moralDao.updateById(moral);
    }
}
